從上一篇 [cURL] 怎麼從程式內部 call api? ,我們已經知道如何用 PHP cURL function 做到基本 call api 方式。 Call API 時,curl_init(), curl_exec(), curl_close() 變化不大,因此,本文敘述 curl_setopt() 常用的 Parameters,諸如 option, value
我們已知 PHP cURL function 是參考 cURL 的 libcurl 製作,因此,curl_setopt() 裡面有許多 PHP 的 option (CURLOPT_XXX) ,欲知詳細內容,請參照 PHP curl_setopt
curl_setopt(CurlHandle $handle, int $option, mixed $value):
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// .. 略
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) 為例,function 會有三個 Parameter,分別為:
$ch 
CURLOPT_RETURNTRANSFER
true
| 選項 | 說明 | value | 
|---|---|---|
| CURLOPT_URL | URL to fetch | string | 
| CURLOPT_RETURNTRANSFER | 返回字串而不是直接輸出 | bool | 
| CURLOPT_CUSTOMREQUEST | custom request method (POST, PATCH..etc) | string | 
| CURLOPT_POSTFIELDS | 設定傳送的檔案 (payload) | string | 
| CURLOPT_HTTPHEADER | 設定 HTTP 檔頭(Header) | array | 
| CURLOPT_HEADER | 輸出 Header | bool | 
1 [cURL] 怎麼從程式內部 call api?
2 PHP curl_setopt
PHP, cURL, and HTTP POST example?